package br.com.radiomir;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;


public class Inicio extends Activity {
	
	final String songs_urls[] = {
			"http://streaming20.brlogic.com:8010/live"};
			
	
	private MediaPlayer mediaPlayer;
	AudioManager audioManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inicio);
		
		ImageButton btn_pause   = (ImageButton) findViewById (R.id.btn_pause);
		ImageButton btn_play    = (ImageButton) findViewById (R.id.btn_play);
		ImageButton btn_stop    = (ImageButton) findViewById (R.id.btn_stop);
		SeekBar     seek_volume = (SeekBar) findViewById (R.id.seek_volume);
		
		 audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
	     int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	     int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
	     seek_volume.setMax(maxVolume);
	     seek_volume.setProgress(curVolume);
	     
	     seek_volume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
				
			}
		});
	     
	     
		
		
		
		mediaPlayer = new MediaPlayer();
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		

		btn_play.setOnClickListener(new View.OnClickListener() {
			
			
			public void onClick(View v) {
				
				try {
					mediaPlayer.setDataSource(songs_urls[0]);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				try {
					mediaPlayer.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				mediaPlayer.start();
				 Toast.makeText(Inicio.this,
				 "Tocando Rádio MIR", Toast.LENGTH_LONG)
				 .show();
				
				
			}
		});
		
	   btn_stop.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			mediaPlayer.stop();
			 Toast.makeText(Inicio.this,
			 "Parando Rádio MIR", Toast.LENGTH_LONG)
			 .show();

				
		}
	});
	
	   btn_pause.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			mediaPlayer.pause();
			 Toast.makeText(Inicio.this,
			 "Pausando Rádio MIR", Toast.LENGTH_LONG)
			 .show();
			
		}
	});
	}
	
}